package com.zg.autoflow.web;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.autoflow.req.AutoflowInherentFlowAddReq;
import com.zg.autoflow.req.AutoflowInherentFlowPageReq;
import com.zg.autoflow.req.AutoflowInherentFlowUpdateReq;
import com.zg.autoflow.req.AutoflowInherentFlowdsReq;
import com.zg.autoflow.service.AutoflowInherentFlowService;
import com.zg.common.core.R;
import com.zg.common.core.dao.BaseEntity;
import com.zg.common.core.dao.Inherent.AutoflowInherentFlow;
import com.zg.common.core.dao.Inherent.dao.AutoflowInherentFlowDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
* @author zg
*/
@RestController
@RequiredArgsConstructor
@Api(tags = {"固有字段流程绑定"})
public class AutoflowInherentFlowController {
	private final AutoflowInherentFlowDao autoflowInherentFlowDao;
	private final AutoflowInherentFlowService autoflowInherentFlowService;

	@PostMapping("/autoflow/inherent/flow/add")
	@ApiOperation("新增固有字段流程绑定")
	public R<?> add(@RequestBody @Valid AutoflowInherentFlowAddReq param) {
		AutoflowInherentFlow obj = autoflowInherentFlowService.add(param);
		return R.success(obj);
	}

	@PostMapping("/autoflow/inherent/flow/del")
	@ApiOperation("删除固有字段流程绑定")
	public R<?> del(@RequestBody @Valid AutoflowInherentFlowdsReq req) {
		autoflowInherentFlowService.del(req.getInherentFlowId());
		return R.success();
	}

	@PostMapping("/autoflow/inherent/flow/update")
	@ApiOperation("修改固有字段流程绑定")
	public R<?> update(@RequestBody @Valid AutoflowInherentFlowUpdateReq param) {
		AutoflowInherentFlow obj = autoflowInherentFlowService.update(param);
		return R.success(obj);
	}

	@GetMapping("/autoflow/inherent/flow/page")
	@ApiOperation("分页查询固有字段流程绑定")
	public R<IPage<AutoflowInherentFlow>> page(@RequestBody @Valid AutoflowInherentFlowPageReq req) {
		LambdaQueryWrapper<AutoflowInherentFlow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		if (StrUtil.isNotBlank(req.getFlowCode())) {
			lambdaQueryWrapper.like(AutoflowInherentFlow::getFlowCode, "%" + req.getFlowCode() + "%");
		}
		if (StrUtil.isNotBlank(req.getFromFields())) {
			lambdaQueryWrapper.like(AutoflowInherentFlow::getFromFields, "%" + req.getFromFields() + "%");
		}
		lambdaQueryWrapper.orderByDesc(BaseEntity::getCreateTime);
		IPage<AutoflowInherentFlow> pb = autoflowInherentFlowDao.page(
			new Page<>(req.getPage(), req.getPageSize()),
			lambdaQueryWrapper);
		return R.success(pb);
	}
}
